package cn.com.connext.isp.library.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @author 小木
 * @create 2020/4/9
 * @desc Excel工具类
 */
@Slf4j
public class ExcelUtils {


    /**
     * 导出Excel
     *
     * @param workbook
     * @param response
     * @return
     */
    public static void exportExcel(Workbook workbook, HttpServletResponse response) {
        try {
            OutputStream out = response.getOutputStream();
            String fileNameStr = "车辆里程报表数据.xls";
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileNameStr, "utf-8"));
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(out);
            bufferedOutPut.flush();
            workbook.write(bufferedOutPut);
            bufferedOutPut.close();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
